package Componand.XmiReader;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Componand.Ireader;
import Model.Classe;
import Model.interfaces.Iassociation;
import Model.interfaces.Idiagrame;

public class XmiReader implements Ireader{
	private Map<String,Classe> ClasMap; 
	private Map<String,Iassociation> associationmap;
	private XmiReaderClassFactory factory =new XmiReaderClassFactory();
private	Idiagrame diagrame;
	File inputFile;
	@Override
	public void Read(File f) {
		
		this.inputFile=f;
		final Pattern pattern = Pattern.compile("<packagedElement(.+?)/>");
		final Pattern patternobjectassoc = Pattern.compile("<packagedElement(.+?)'>");
		
		
		
		FileInputStream inp;
		try {
		inp = new FileInputStream(f);
		DataInputStream in = new DataInputStream(inp);
		BufferedReader read = new BufferedReader(new InputStreamReader(in));
		 
		 String strLine;
		 String strLine1 = "";

		while (( strLine = read.readLine()) != null)   {
			 
			strLine1+=strLine+"\n";
		 }
	Matcher typeclasses=pattern.matcher(strLine1);
	while(typeclasses.find()){
		
		factory.addClas(typeclasses.group(1));
		
		
		
		
	}
Matcher associationM= patternobjectassoc.matcher(strLine1);
while(associationM.find()){
	
	factory.addAssociation(associationM.group(1));
}
factory.addAssosiationData(strLine1);
		
setDiagrame(factory.create());
		
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		}catch(IOException ieo){
			
		}
			
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	}

	@Override
	public Idiagrame convert() {
		
		return diagrame;
	}

	public Idiagrame getDiagrame() {
		return diagrame;
	}

	public void setDiagrame(Idiagrame diagrame) {
		this.diagrame = diagrame;
	}

}
